package com.zoe.app.entity;

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;

import java.util.Date;

/**
 * @author Zoe.Zhang
 * @date 2021/09/25
 * @description
 */
// 批量忽略属性 - 如果字段上已经有了Jackson注解也会导致该字段无法忽略
@JsonIgnoreProperties({
        "like", "job"
})
// 忽略null值,可以配置在类or属性上
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class User {
    // 指定序列化成字符串
    @JsonSerialize(using = ToStringSerializer.class)
    // JsonProperty:
    //  value > 指定序列化/反序列化时候  展示给前端/前端传给后台Json字符  所使用的属性名
    //  index > 指定序列化时候的展示顺序,数字越大,优先级越往后
    @JsonProperty(value = "userAge", index = 99)
    private Integer age;
    @JsonProperty(index = 1)
    private String userName;

    // JsonFormat: 优先级大于全局配置
    //  pattern > 指定序列化/反序列化时候  展示/解析  所使用的格式
    //  timezone > 指定序列化/反序列化时候  展示/解析  所使用的时区
    //  pattern = "yyyy-MM-dd" 可以接受yyyy-MM-dd HH:mm:ss 格式的数据,但是后面的时分秒不会被解析
    //  pattern = "yyyy-MM-dd HH:mm:ss" 只可以接受yyyy-MM-dd HH:mm:ss 格式的数据
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date birthday;

    // 指定序列化/反序列化时候  展示/解析  忽略该属性 - 不能搭配其他Jackson注解使用,否则会导致无法忽略
    @JsonIgnore
    private String address;

    @JsonProperty(value = "LIKE", index = 2)
    private String like;

    private String job;
    private Date createTime;

    // is开头的非静态public方法会被自动序列化成 方法名去掉is之后的属性
    public boolean isActive() {
        return true;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public User setCreateTime(Date createTime) {
        this.createTime = createTime;
        return this;
    }

    public static User Builder() {
        return new User();
    }

    public User build() {
        return this;
    }

    public Integer getAge() {
        return age;
    }

    public User setAge(Integer age) {
        this.age = age;
        return this;
    }

    public String getUserName() {
        return userName;
    }

    public User setUserName(String userName) {
        this.userName = userName;
        return this;
    }

    public Date getBirthday() {
        return birthday;
    }

    public User setBirthday(Date birthday) {
        this.birthday = birthday;
        return this;
    }

    public String getAddress() {
        return address;
    }

    public User setAddress(String address) {
        this.address = address;
        return this;
    }

    public String getLike() {
        return like;
    }

    public User setLike(String like) {
        this.like = like;
        return this;
    }

    public String getJob() {
        return job;
    }

    public User setJob(String job) {
        this.job = job;
        return this;
    }

    @Override
    public String toString() {
        return "User{" +
                "age=" + age +
                ", userName='" + userName + '\'' +
                ", birthday=" + birthday +
                ", address='" + address + '\'' +
                ", like='" + like + '\'' +
                ", job='" + job + '\'' +
                ", createTime=" + createTime +
                '}';
    }
}
